package com.dissertation.action;

import com.dissertation.model.ApplyAllow;
import com.dissertation.pojo.Message;
import com.dissertation.respository.ApplyAllowRepository;
import com.dissertation.service.IApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("apply")
public class ApplyController {

    @Autowired
    ApplyAllowRepository applyAllowRepository;

    @Autowired
    IApplyService applyService;

    /**
     * 获取别人向自己发送的申请
     * @param owner
     * @return
     */
    @GetMapping("owner")
    public Message getApply(@RequestParam String owner){
        List<ApplyAllow> applyAllowList = applyAllowRepository.findByOwner(owner);
        return Message.createSuc(applyAllowList);
    }


    /**
     * 查询自己发出的申请
     * @param applyName
     * @return
     */

    @GetMapping("send")
    public Message getApply2(@RequestParam String applyName){
        List<ApplyAllow> applyAllowList = applyAllowRepository.findByApplyName(applyName);
        return Message.createSuc(applyAllowList);
    }



    /**
     * 添加申请
     * @param applyAllow
     * @return
     */
    @PutMapping
    public Message addApply(@RequestBody ApplyAllow applyAllow){
        return applyService.addApply(applyAllow);
    }

    /**
     * 处理申请，
     * @param id 申请的id
     * @param result 处理结果，true为同意，FALSE为不同意
     * @return
     */

    @PostMapping
    public Message alterApply(@RequestParam String id,@RequestParam boolean result,@RequestParam(required = false) Integer valid){
        return applyService.alterApply(id,result,valid);
    }

    /**
     * 删除申请
     * @param ids
     * @return
     */
    @DeleteMapping
    public Message deleApply(@RequestParam String[] ids){
        return applyService.deleApply(ids);
    }


}
